package com.zusmart.core.condition.extend;

import java.util.Map;

import com.zusmart.base.util.StringUtils;
import com.zusmart.core.bean.BeanDefinition;
import com.zusmart.core.condition.Condition;
import com.zusmart.core.condition.ConditionContext;
import com.zusmart.core.config.ConfigSourceManager;
import com.zusmart.core.plugin.PluginManager;

public class ConditionalOnHasClassRule implements Condition {

	@Override
	public boolean matches(ConditionContext conditionContext, Map<String, BeanDefinition> beanDefinitions, BeanDefinition targetBeanDefinition, PluginManager pluginManager, ConfigSourceManager configSourceManager) {
		ConditionalOnHasClass hasClass = (ConditionalOnHasClass) conditionContext.getExtendAnnotation();
		String clazz = hasClass.value();
		return this.doMatches(conditionContext, beanDefinitions, targetBeanDefinition, clazz);
	}

	protected boolean doMatches(ConditionContext conditionContext, Map<String, BeanDefinition> beanDefinitions, BeanDefinition targetBeanDefinition, String clazz) {
		if (StringUtils.isBlank(clazz)) {
			return true;
		}
		try {
			Class.forName(clazz);
			return true;
		} catch (ClassNotFoundException e) {
			return false;
		}
	}

}